package service

import (
	promoteSrv "git.huoys.com/block_chain/mall/internal/promote/service"
	"git.huoys.com/block_chain/mall/internal/user/model"
	"git.huoys.com/block_chain/mall/internal/user/repository"
	"git.huoys.com/block_chain/mall/pkg/log"
)

type UserService interface {
	/*用户相关*/
	// 创建用户
	CreateUser(phone, phoneArea, language, pwd string) (uid, wid uint64, err error)
	// 注销账户
	DelUser(uid uint64) (err error)
	// 判断手机号是否存在
	ExistPhone(phone string) (exist bool, err error)
	// 修改帐号信息
	UpdateUser(user model.User) (err error)
	// 匹配密码
	CheckPassword(phone string, password string) (userInfo model.UserInfo, err error)
	// 获取单个用户信息
	GetUser(uid uint64) (user model.User, err error)
	// 获取多个用户信息
	GetUserS(uids []uint64) (users []model.User, err error)
	// 绑定邀请码
	BindingInvitationCode(uid, code uint64) (success bool, err error)
	// 更新钱包信息
	SetWallet(wallet model.Wallet) (err error)
	// 获取钱包信息
	GetWallet(wid uint64) (wallet model.Wallet, balances string, err error)
	// 获取钱包信息通过uid
	GetWalletByUid(uid uint64) (wallet model.Wallet, err error)

	// 转账
	Transaction(fromUid, toUid, orderId uint64, addr string, price float32) (hash string, err error)
	// 查询转账记录
	GetTransactionRecord(uid uint64) (record []model.OrderTransferRecord, err error)

	// 获取自己下级
	GetDescendantByUid(uid uint64) (superior model.User, err error)
	// 获取自己全部下级
	GetAllDescendantByUid(uid uint64) (users []model.User, err error)
	// 获取自己上级
	GetAncestorByUid(uid uint64) (superior model.User, err error)
	// 获取自己全部上级
	GetAllAncestorByUid(uid uint64) (users []model.User, err error)

	IssueToken(uid uint64, device string) (token string, err error)
	UpdateToken(token string) (NewToken string, err error)
	VerifyToken(token string) (VerifyResult bool, Uid uint64, err error)

	// 关闭数据库
	Close()
}

type userService struct {
	log        *log.Logger
	rpo        repository.UserRepository
	promoteSrv promoteSrv.Service
}

func NewService(log *log.Logger, rpo repository.UserRepository, promoteSrv promoteSrv.Service) UserService {
	s := &userService{
		log:        log,
		rpo:        rpo,
		promoteSrv: promoteSrv,
	}
	return s
}

func (s *userService) Close() {
	s.rpo.Close()
}
